package com.company.笔试;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Classname 多线程排序
 * @Description TODO
 * @Date 2021/8/11 16:20
 * @Created by ylr
 */
public class 多线程排序 {

    public static volatile Queue<Integer> res = new PriorityBlockingQueue<>(3, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1-o2;//升序
        }
    });

    public static volatile Integer min = Integer.MAX_VALUE;

    public static void main(String[] args) {
        Lock lock = new ReentrantLock();

        new Thread(
                ()->{
                    int[] th1 = {3,6,8};
                    for (int i : th1) {
                        while (true){
                            if (i<min){
                                min = i;
                                try {
                                    min.wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            if (min==i){
                                System.out.println(min);
                                break;
                            }
                        }
                    }
                }
        ).start();

        new Thread(
                ()->{
                    int[] th2 = {5,6,7};
                    for (int i : th2) {
                        while (true){
                            if (i<min){
                                min = i;
                                Thread.yield();
                            }
                            if (min==i){
                                System.out.println(min);
                                break;
                            }
                        }
                    }
                }
        ).start();

        new Thread(
                ()->{
                    int[] th3 = {1,4,7,8};
                    for (int i : th3) {
                        while (true){
                            if (i<min){
                                min = i;
                                Thread.yield();
                            }
                            if (min==i){
                                System.out.println(min);
                                break;
                            }
                        }
                    }
                }
        ).start();
//        res.stream().sorted().collect(Collectors.toList()).forEach(System.out::println);


    }


}
